import sqlite3
from DBInit import DBInit
from Login import Login
from Regist import Regist


#Init database ,users(id,name,password)/record(data,name,salary,rctime)
#commit()/fetchall()

dbpath='salary.db'
DBInit(dbpath)

print("Welcome to Salary Notebook!")

while True:
    command=input('Command: L\l(login),R\r(regist),Q\q(quit)>>>')

    if command in ('L','l'):
        conn,cursor=Login(dbpath)
        if conn:
            print("conn/cursor to tables record")
            while True:
                operation=input('Operation: Select,Insert,Update,Delete...SQL>>>')
                if operation in ('q','quit','Q','Quit','QUIT'):
                    print('Quit your notebook.')
                    break
                if operation=="help":
                    print("conn=sqlite3.connect(db_path)\n\
cursor=conn.cursor()\n\
tables:users(id,name,password),record(date,name,salary,rctime)")
                else:
                    try :
                        cursor.execute(operation)
                        print(cursor.fetchall())
                        conn.commit()
                    except sqlite3.OperationalError:
                        print('SQL Error')
                        
    elif command in ('R','r'):
        Regist(dbpath)

    elif command in ('Q','q'):
        break

    else:
        print('No such command: %s'%command)
